﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz.Spi;
using Quartz.Net.Model;
using Quartz.Net.Factory;
using Newtonsoft.Json;

namespace Quartz.Net.Core.Listener
{
   public class myJobListener:IJobListener
    {
       private string name ;
       public string Name { get { return "myJobListener"; } set { name = value; } }
       /// <summary>
        /// 触发器 VetoJobExecution 为true时触发该方法（触发器否决时，任务后面的不再执行）
       /// </summary>
       /// <param name="context"></param>
        public void JobExecutionVetoed(IJobExecutionContext context)
        {
            //throw new NotImplementedException();
            //JobDataMap dataMap = context.JobDetail.JobDataMap;
            //string JobName = dataMap.GetString("jobName");
            //Console.WriteLine(JobName+":1111JobExecutionVetoed");
        }
       /// <summary>
       /// 触发器调用，任务开始执行时，触发该方法
       /// </summary>
       /// <param name="context"></param>
        public void JobToBeExecuted(IJobExecutionContext context)
        {
            //在Quartz.IJobDetail即将执行时由Quartz.IScheduler调用
            //
            //如果作业的执行被否决，则不会调用此方法
            //
            //throw new NotImplementedException();
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            Task_Quartz_JobTriggerModel list = JsonConvert.DeserializeObject<Task_Quartz_JobTriggerModel>(dataMap.GetString("jobDetails"));
            Console.WriteLine(list.JOBNAME+ ":JobToBeExecuted");
            factoryClass.JobToBeExecuted(list.JOBNAME);
            factoryClass.UpdateRunStatus(list.NGUID, RunStatus.Run);
        }
       /// <summary>
        /// 任务完成时触发该方法，触发器还没执行完成，下一步到TriggerComplete，触发器完成（即任务真正完成）
       /// </summary>
       /// <param name="context"></param>
       /// <param name="jobException"></param>
        public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
        {
            //throw new NotImplementedException();
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            Task_Quartz_JobTriggerModel list = JsonConvert.DeserializeObject<Task_Quartz_JobTriggerModel>(dataMap.GetString("jobDetails"));
            Console.WriteLine(list.JOBNAME + ":JobWasExecuted");
            factoryClass.JobWasExecuted(list.JOBNAME);
            if (list.JOBNAME == "JobLj1")
            {
                factoryClass.UpdateRunStatus(list.NGUID, RunStatus.Suspend);
            }
            else if (list.JOBNAME == "jobts1")
            {
                
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(list.JOBNAME))
                {
                    factoryClass.UpdateRunStatus(list.NGUID, RunStatus.Wait);
                }
                
            }
           
        }

       
    }
}
